﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DroidLord.app.dy.data
{

    class DYUserGoodList
    {
        //返回是否正确
		/// <summary>
        /// 是否返回数据成功
        /// </summary>
        public bool isResponseOk = false;
        //解析是否正确
        public bool isParseOk = false;
        public List<DYUserGoodItem> itemList = new List<DYUserGoodItem>();
		/// <summary>
        /// 商品总数
        /// </summary>
        public string total = "0";

        public bool hasMore = false;


        private static string getValue(JObject myobj, string key)
        {
            try
            {
                return myobj[key].ToString();
            }
            catch (Exception eee)
            {

            }
            return "";
        }
        private static DYUserGoodItem parseItem(string itemJson)
        {
            DYUserGoodItem itemObj = new DYUserGoodItem();
            var myobj = JObject.Parse(itemJson);
            itemObj.videoId = getValue(myobj, "last_aweme_id");
            itemObj.detailUrl = getValue(myobj, "detail_url");
            string visitorstring = getValue(myobj, "visitor");
            if(!visitorstring.Equals(""))
            {
                var visitorObj = JObject.Parse(visitorstring);
                string count = getValue(visitorObj, "count");
                itemObj.visitorCount = count;
            }
            itemObj.marketPrice = getValue(myobj, "market_price");
            string iamgesstring = getValue(myobj, "images");
            if(!iamgesstring.Equals(""))
            {
                JArray myarray = JArray.Parse(iamgesstring);
                int len = myarray.Count;
                for(int i=0;i<len;i++)
                {
                    string iamgeitem=myarray[i].ToString();
                    bool isGetOk = false;
                    if(!iamgeitem.Equals(""))
                    {
                        string urlliststring = JObject.Parse(iamgeitem)["url_list"].ToString();
                        if(!urlliststring.Equals(""))
                        {
                            JArray urllist = JArray.Parse(urlliststring);
                            int mylen = urllist.Count;
                            for(int j=0;j<mylen;j++)
                            {
                                itemObj.goodImage = urllist[0].ToString();
                                isGetOk = true;
                                break;
                            }
                        }
                    }
                    if(isGetOk)
                    {
                        break;
                    }
                }
            }
            itemObj.goodsSource = getValue(myobj, "goods_source");
            itemObj.productId = getValue(myobj, "product_id");
            itemObj.minPrice = getValue(myobj, "min_price");
            itemObj.sales = getValue(myobj, "sales");
            itemObj.title = getValue(myobj, "title");
            itemObj.price = getValue(myobj, "price");
            return itemObj;
        }

        public static  DYUserGoodList parse(string popluarInfo)
        {
            DYUserGoodList itemList = new DYUserGoodList();
            var myobj = JObject.Parse(popluarInfo);
            string code = getValue(myobj, "status_code");
            if (code.Equals("0"))
            {
                itemList.isResponseOk = true;
                itemList.isParseOk = true;
                string datastring = getValue(myobj, "promotions");
                string hasmore = getValue(myobj, "has_more");
                itemList.total = getValue(myobj, "count");
                if(hasmore.Trim().ToLower().Equals("true".ToLower()))
                {
                    itemList.hasMore = true;
                }
                if (!datastring.Equals(""))
                {
                    JArray myarray = JArray.Parse(datastring);
                    int len = myarray.Count;
                    for (int i = 0; i < len; i++)
                    {
                        string itemstring = myarray[i].ToString();
                        DYUserGoodItem itemDy = parseItem(itemstring);
                        itemList.itemList.Add(itemDy);
                    }
                }

            }
            else
            {
                itemList.isResponseOk = false;
            }
            return itemList;
        }

    }
    class DYUserGoodItem
    {


        //
        public string title = "";
        //商品图片
        //
        public string goodImage = "";


        //商品详情url
        public string detailUrl = "";
        //商品ID
        public string productId = "";

        //商品最低价
        public string minPrice = "";
        //市场价，原价
        public string marketPrice = "";

        //销售价
        public string price = "";



        //销量
        public string sales = "0";


        //商品访问量
        public string visitorCount = "";

        //商品关联视频ID
        public string videoId = "";
        
       
       
       
		//商品来源
        public string goodsSource = "";
		
       
       
       

        
    }
}
